package com.sz.app.edi.job.email;

import com.sz.common.base.logger.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * Package: com.sz.app.edi.job.email <br>
 * Function: TODO: ADD FUNCTION <br>
 * Author: Charles. <br>
 * Date: 2017-04-10 16:30:00.
 */
@Service
public class EmailService {
    @Autowired
    private EmailConfig emailConfig;
    @Autowired
    private JavaMailSender mailSender;
    Logger logger = Logger.getLogger(this.getClass());
    /**
     * @param sendTo
     * @param title
     * @param content
     */
    public void sendSimpleMail(String title, String content,String ...sendTo) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(emailConfig.getEmailFrom());
        message.setTo(sendTo);
        message.setSubject(title);
        message.setText(content);
        mailSender.send(message);
    }

    public void sendHtmlMail(String title, String content,String ...sendTo) throws MessagingException {
        logger.info("====6.2=====");
        MimeMessage message = mailSender.createMimeMessage();
        logger.info("====6.3====="+message);
        MimeMessageHelper helper = new MimeMessageHelper(message);
        logger.info("====6.4====="+helper);
        helper.setFrom(emailConfig.getEmailFrom());
        logger.info("====6.5====="+emailConfig.getEmailFrom());
        helper.setTo(sendTo);
        logger.info("====6.6====="+sendTo);
        helper.setSubject(title);
        logger.info("====6.7====="+title);
        helper.setText(content, true);
        logger.info("====6.8====="+content);
        mailSender.send(message);
        logger.info("====6.9====="+message);
    }
}
